/**
 * output package name
 */
package com.kingdee.eas.custom.wg.reports.client;

import java.awt.event.*;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.ctrl.swing.event.DataChangeEvent;
import com.kingdee.bos.ctrl.swing.event.DataChangeListener;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.basedata.assistant.CurrencyFactory;
import com.kingdee.eas.basedata.assistant.CurrencyInfo;
import com.kingdee.eas.basedata.framework.util.EntityControlTypeUtil;
import com.kingdee.eas.basedata.master.material.IMaterialGroupStandard;
import com.kingdee.eas.basedata.master.material.MaterialGroupStandardCollection;
import com.kingdee.eas.basedata.master.material.MaterialGroupStandardFactory;
import com.kingdee.eas.basedata.master.material.MaterialGroupStandardInfo;
import com.kingdee.eas.basedata.master.material.MaterialGroupStandardTypeEnum;
import com.kingdee.eas.basedata.org.CtrlUnitInfo;
import com.kingdee.eas.basedata.org.FullOrgUnitInfo;
import com.kingdee.eas.basedata.org.OrgType;
import com.kingdee.eas.basedata.org.client.f7.SCMRptMultiOrgsF7;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.framework.*;
import com.kingdee.eas.framework.report.util.RptParams;
import com.kingdee.eas.scm.im.inv.client.InvClientUtils;
import com.kingdee.eas.scm.im.rpt.IMRptUtils;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.MsgBox;

/**
 * output class name
 */
public class StockTransferRptConditionUI extends AbstractStockTransferRptConditionUI
{

    public StockTransferRptConditionUI()
        throws Exception
    {
        firstLoad = true;
        f7 = null;
        firstCur = null;
        amtPrecision = 4;
        addCkBxSimpleNameListener();
        prmtStorage.setQueryInfo("com.kingdee.eas.basedata.org.app.StorageOrgUnitQuery");
    }

    private void loadGroupStandard()
        throws Exception
    {
        MaterialGroupStandardInfo groupStandard = null;
        Boolean onlyBaseStandard = (Boolean)getUIContext().get("onlyBaseStandard");
        if(onlyBaseStandard != null && onlyBaseStandard.booleanValue())
        {
            IMaterialGroupStandard iBiz = MaterialGroupStandardFactory.getRemoteInstance();
            MaterialGroupStandardCollection baseGroupStandardColl = iBiz.getBaseMaterialGroupStandardColl();
            if(baseGroupStandardColl.size() > 0)
                groupStandard = baseGroupStandardColl.get(0);
        } else
        {
            groupStandard = (MaterialGroupStandardInfo)getUIContext().get("groupStandard");
        }
        if(groupStandard != null)
        {
            comboGroupStandard.removeAllItems();
            comboGroupStandard.addItem(groupStandard);
            comboGroupStandard.setSelectedIndex(0);
            groupStandardID = groupStandard.getId().toString();
        } else
        {
            IMaterialGroupStandard iMaterialGroupStandard = MaterialGroupStandardFactory.getRemoteInstance();
            com.kingdee.bos.metadata.entity.FilterInfo filterInfoCU = EntityControlTypeUtil.getFilterInfoForControlTypeS4(curCtrlUnitInfo.getId().toString(), curCtrlUnitInfo.getLongNumber());
            EntityViewInfo viewInfo = new EntityViewInfo();
            viewInfo.setFilter(filterInfoCU);
            MaterialGroupStandardCollection groupStandardColl = iMaterialGroupStandard.getMaterialGroupStandardCollection(viewInfo);
            comboGroupStandard.removeAllItems();
            if(groupStandardColl != null)
                comboGroupStandard.addItems(groupStandardColl.toArray());
            int defaultIndex = 0;
            int size = groupStandardColl.size();
            int index = 0;
            do
            {
                if(index >= size)
                    break;
                if(groupStandardColl.get(index).getStandardType().equals(MaterialGroupStandardTypeEnum.BasicStandard))
                {
                    defaultIndex = index;
                    break;
                }
                index++;
            } while(true);
            comboGroupStandard.setSelectedIndex(defaultIndex);
            MaterialGroupStandardInfo groupStandardInfo = groupStandardColl.get(defaultIndex);
            groupStandardID = groupStandardInfo.getId().toString();
        }
    }

    public void onLoad()
        throws Exception
    {
        super.onLoad();
        if(firstLoad)
        {
            comboGroupStandard.addItemListener(new ItemListener() {

                public void itemStateChanged(ItemEvent e)
                {
                    comboGroupStandard_itemStateChanged(e);
                }

            
           
            }
);
            curCtrlUnitInfo = (CtrlUnitInfo)getUIContext().get("cu");
            if(curCtrlUnitInfo == null)
                curCtrlUnitInfo = SysContext.getSysContext().getCurrentCtrlUnit();
            loadGroupStandard();
            InvClientUtils.getMaterialF7ForReportFilterUI(prmtMaterialFrom, this);
            InvClientUtils.getMaterialF7ForReportFilterUI(prmtMaterialTo, this);
            f7 = new SCMRptMultiOrgsF7(prmtStorage, OrgType.Storage, "stocktransferrpt_view", EASResource.getString("com.kingdee.eas.scm.im.rpt.IMRptResource", "storageOrgUnitMutilF7"));
            prmtStorage.addDataChangeListener(new DataChangeListener() {

                public void dataChanged(DataChangeEvent eventObj)
                {
                    fullStorageOrgUnit = (Object[])f7.getFullData();
                }

            
           
            }
);
            initDateCom();
            kDRadioAll.setSelected(true);
            firstLoad = false;
        }
    }

    private void comboGroupStandard_itemStateChanged(ItemEvent e)
    {
        Object o = e.getItem();
        MaterialGroupStandardInfo groupInfo = null;
        if(o != null && (o instanceof MaterialGroupStandardInfo))
            groupInfo = (MaterialGroupStandardInfo)o;
        if(prmtMaterGrFrom.getValue() != null || prmtMaterGrTo.getValue() != null)
        {
            prmtMaterGrFrom.setValue(null);
            prmtMaterGrTo.setValue(null);
        }
        IMRptUtils.setBizMaterialGroup(prmtMaterGrFrom, groupInfo.getId().toString());
        IMRptUtils.setBizMaterialGroup(prmtMaterGrTo, groupInfo.getId().toString());
    }

    public void storeFields()
    {
        super.storeFields();
    }

    public void onInit(RptParams rptparams)
        throws Exception
    {
    }

    public void clear()
    {
        prmtMaterGrFrom.setValue(null);
        prmtMaterGrTo.setValue(null);
        prmtMaterialFrom.setValue(null);
        prmtMaterialTo.setValue(null);
        prmtStorage.setValue(null);
        ckBxByStatUnit.setSelected(false);
        ckBxShowMShortName.setSelected(false);
        txtMShortNameFrom.setText(null);
        txtMShortNameTo.setText(null);
        initDateCom();
    }

    private void initDateCom()
    {
        Date date = new Date();
        dateTo.setValue(IMRptUtils.getDate(date));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(7, -6);
        calendar.set(calendar.get(1), calendar.get(2), calendar.get(5), 23, 59, 59);
        dateFrom.setValue(new Timestamp(calendar.getTimeInMillis()));
    }

    public RptParams getCustomCondition()
    {
        RptParams para = new RptParams();
        para.setObject("dateFrom", dateFrom.getValue());
        para.setObject("dateTo", dateTo.getValue());
        para.setObject("materialGroupStandard", comboGroupStandard.getSelectedItem());
        para.setObject("materialFrom", prmtMaterialFrom.getValue());
        para.setObject("materialTo", prmtMaterialTo.getValue());
        para.setObject("matGrFrom", prmtMaterGrFrom.getValue());
        para.setObject("matGrTo", prmtMaterGrTo.getValue());
        para.setObject("sotrageOrg", prmtStorage.getValue());
        para.setObject("fullSotrageOrg", ((Object) (fullStorageOrgUnit)));
        amtPrecision = 4;
        if(firstCur != null)
            try
            {
                CurrencyInfo currency = CurrencyFactory.getRemoteInstance().getCurrencyInfo(new ObjectUuidPK(firstCur));
                if(currency != null)
                    amtPrecision = currency.getPrecision();
            }
            catch(EASBizException e)
            {
                handUIException(e);
            }
            catch(BOSException e)
            {
                handUIException(e);
            }
        para.setInt("amtPrecision", amtPrecision);
        para.setObject("ckBxByStatUnit", Boolean.valueOf(ckBxByStatUnit.isSelected()));
        para.setObject(kDRadioAll.getName(), Boolean.valueOf(kDRadioAll.isSelected()));
        para.setObject(kDRadioOrder.getName(), Boolean.valueOf(kDRadioOrder.isSelected()));
        para.setObject(kDRadioStock.getName(), Boolean.valueOf(kDRadioStock.isSelected()));
        para.setObject("ckBxShowMShortName", Boolean.valueOf(ckBxShowMShortName.isSelected()));
        para.setObject("shortNameFrom", txtMShortNameFrom.getText());
        para.setObject("shortNameTo", txtMShortNameTo.getText());
        return para;
    }

    public void setCustomCondition(RptParams params)
    {
        dateFrom.setValue(params.getObject("dateFrom"));
        dateTo.setValue(params.getObject("dateTo"));
        prmtMaterialFrom.setValue(params.getObject("materialFrom"));
        prmtMaterialTo.setValue(params.getObject("materialTo"));
        comboGroupStandard.setSelectedItem(params.getObject("materialGroupStandard"));
        prmtMaterGrFrom.setValue(params.getObject("matGrFrom"));
        prmtMaterGrTo.setValue(params.getObject("matGrTo"));
        prmtStorage.setValue(params.getObject("sotrageOrg"));
        amtPrecision = params.getInt("amtPrecision");
        ckBxByStatUnit.setSelected(((Boolean)params.getObject("ckBxByStatUnit")).booleanValue());
        kDRadioAll.setSelected(((Boolean)params.getObject(kDRadioAll.getName())).booleanValue());
        kDRadioOrder.setSelected(((Boolean)params.getObject(kDRadioOrder.getName())).booleanValue());
        kDRadioStock.setSelected(((Boolean)params.getObject(kDRadioStock.getName())).booleanValue());
        ckBxByStatUnit.setSelected(((Boolean)params.getObject("ckBxByStatUnit")).booleanValue());
        if((Boolean)params.getObject("ckBxShowMShortName") != null)
            ckBxShowMShortName.setSelected(((Boolean)params.getObject("ckBxShowMShortName")).booleanValue());
        else
            ckBxShowMShortName.setSelected(false);
        txtMShortNameFrom.setText((String)params.getObject("shortNameFrom"));
        txtMShortNameTo.setText((String)params.getObject("shortNameTo"));
    }

    public String getUITitle()
    {
        return InvClientUtils.getResource("StockTransferMaterialRPT") + "-" + InvClientUtils.getResource("Query");
    }

    private void showError(String error)
    {
        MsgBox.showError(EASResource.getString("com.kingdee.eas.scm.im.rpt.IMRptResource", error));
    }

    public boolean verify()
    {
        if(prmtStorage.getValue() == null)
        {
            showError("storageOrgUnitNotNull");
            return false;
        }
        if(prmtStorage.getValue() instanceof Object[])
        {
            Object temp[] = (Object[])prmtStorage.getValue();
            if(temp == null || temp.length == 0 || temp.length == 1 && temp[0] == null)
            {
                showError("storageOrgUnitNotNull");
                return false;
            }
            String storageOrgIDs[] = new String[fullStorageOrgUnit.length];
            int i = 0;
            for(int size = fullStorageOrgUnit.length; i < size; i++)
                storageOrgIDs[i] = ((FullOrgUnitInfo)fullStorageOrgUnit[i]).getId().toString();

            try
            {
                if(!IMRptUtils.checkSameCurrency(storageOrgIDs))
                    return false;
            }
            catch(BOSException e)
            {
                super.handUIException(e);
            }
        }
        Date datefrom = (Date)dateFrom.getValue();
        Date dateto = (Date)dateTo.getValue();
        if(datefrom == null)
        {
            showError("STARTDATE_NOT_NULL");
            return false;
        }
        if(dateto == null)
        {
            showError("END_DATE_NULL");
            return false;
        }
        if(dateto.compareTo(datefrom) < 0)
        {
            showError("END_BEFORE_START");
            return false;
        } else
        {
            Date bdate = IMRptUtils.getDate(dateto);
            dateTo.setValue(new Date((bdate.getTime() + 86400000L) - 1L));
            return true;
        }
    }

    protected void addCkBxSimpleNameListener()
    {
        ckBxShowMShortName.setVisible(true);
        txtMShortNameFrom.setVisible(true);
        txtMShortNameTo.setVisible(true);
        ckBxShowMShortName.setSelected(false);
        txtMShortNameFrom.setEnabled(false);
        txtMShortNameTo.setEnabled(false);
        ckBxShowMShortName.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e)
            {
                try
                {
                    ckBxShowMShortName_itemStateChanged(e);
                }
                catch(Exception exc)
                {
                    handUIException(exc);
                }
               
            }

          
        }
);
    }

    protected void ckBxShowMShortName_itemStateChanged(ItemEvent e)
        throws Exception
    {
        boolean isSelected = ckBxShowMShortName.isSelected();
        txtMShortNameFrom.setText(null);
        txtMShortNameTo.setText(null);
        if(isSelected)
        {
            txtMShortNameFrom.setEnabled(true);
            txtMShortNameTo.setEnabled(true);
        } else
        {
            txtMShortNameFrom.setEnabled(false);
            txtMShortNameTo.setEnabled(false);
        }
    }

    private static final long serialVersionUID = -5269143866497858337L;
    private boolean firstLoad;
    private SCMRptMultiOrgsF7 f7;
    private Object fullStorageOrgUnit[];
    private String firstCur;
    private int amtPrecision;
    private static final String resClassName = "com.kingdee.eas.scm.im.rpt.IMRptResource";
    private static final String permItem = "stocktransferrpt_view";
    public static final String CTX_ONLY_BASE_SRANDARD = "onlyBaseStandard";
    public static final String CTX_GROUP_STANDARD = "groupStandard";
    public static final String CTX_CU = "cu";
    private String groupStandardID;
    private CtrlUnitInfo curCtrlUnitInfo;



}